<template>
  <el-dialog
    title="发出样品"
    :visible.sync="dialogFormVisible"
    width="50%"
    @close="back"
  >
    <div class="con" v-loading="!robotId" element-loading-text="正在呼叫小车中">
      <el-result icon="success" style="padding: 0px" v-if="robotId">
      </el-result>

      <div
        style="color: #64c43c; font-size: 24px; margin: 10px 0px"
        v-if="robotId"
      >
        已成功呼叫AMR
      </div>

      <div style="display: flex" v-if="robotId">
        <div style="font-size: 18px; margin: 20px 10px">
          任务编号:
          <span style="color: #dc041c; font-size: 22px">{{
            amrMissionId
          }}</span>
        </div>
        <div style="font-size: 18px; margin: 20px 10px">
          小车编号:
          <span style="color: #dc041c; font-size: 22px">{{ robotId }}</span>
        </div>
      </div>
      <div style="margin-top: 20px" v-if="robotId">
        <el-button type="danger" @click="cancel" :loading="loading"
          >取消任务</el-button
        >
        <el-button type="primary" :loading="loading2" @click="handleConfirmLoad"
          >确认装载</el-button
        >
      </div>
    </div>
  </el-dialog>
</template>

<script>
import {
  sampleMove,
  cancelTask,
  getAmrDw,
  artificialAMR,
  getAmrTask,
} from "@/common/api";

export default {
  name: "send-Dialog",
  props: {},
  components: {},
  data() {
    return {
      dialogFormVisible: false,
      timer: null,
      item: {},
      amrMissionId: "",
      robotId: "",
      loading: false,
      loading2: false,
    };
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    handleAMR() {
      sampleMove({ samNumber: this.item.samNumber }).then((res) => {
        if (res.success) {
          this.amrMissionId = res.data.amrMissionId;
          this.selectInfo();
          this.timer = setInterval(() => {
            //根据任务编号轮询查小车信息
            this.selectInfo();
          }, 3000);
        } else {
          this.$errorMsg(res.message);
        }
      });
    },
    selectInfo() {
      getAmrTask({ id: this.amrMissionId }).then((res) => {
        if (res.success) {
          let json = JSON.parse(res.data);
          console.log(json, "9999999");
          this.robotId = json.robot_id;
          if (this.robotId != 0) {
            this.close();
          }
        } else {
          //   this.$errorMsg(res.message);
        }
      });
    },
    handleConfirmLoad() {
      //确认装载
      let params = {
        robotId: this.robotId,
        room: JSON.parse(localStorage.getItem("loginInfo")).testRoom,
      };
      this.loading2 = true;
      getAmrDw(params).then((res) => {
        //先查询点位信息,再释放小车
        if (res.success) {
          this.outCar(res.data.targetName);
        } else {
          this.loading2 = false;
          this.$errorMsg(res.message);
        }
      });
    },
    outCar(targetName) {
      artificialAMR({
        missionId: this.amrMissionId,
        targetCode: targetName,
      }).then((res) => {
        this.loading2 = false;
        if (res.success) {
          this.$successMsg("操作成功");
          this.dialogFormVisible = false;
        } else {
          this.$errorMsg(res.message);
        }
      });
    },

    cancel() {
      this.loading = true;
      cancelTask({ missionId: this.amrMissionId }).then((res) => {
        this.loading = false;
        if (res.success) {
          this.dialogFormVisible = false;
          this.$successMsg("取消成功");
        } else {
          this.$errorMsg(res.message);
        }
      });
    },

    back() {
      this.close();
    },
    close() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
  },
};
</script>


<style lang="scss" scoped>
.con {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 280px;
}
</style>
